#!/bin/bash

source /etc/os-release

V_DEVICE=$(tr -d '\0' < /sys/firmware/devicetree/base/model)
V_SYSTEM=$(uname -rs)
V_ARCH=$(uname -m)
if [ -f /storage/.config/device ]; then
  R_DEVICE=$(cat /storage/.config/device)
fi

# SoC type
if [[ ${V_DEVICE} =~ RG552 ]]; then
  V_SOC="Rockchip RK3399"
else
  V_SOC="Rockchip RK3326"
fi

# CPU model
if [[ ${V_DEVICE} =~ RG552 ]]; then
  V_CPU_MODEL="ARM Cortex A53/Cortex A72"
else
  V_CPU_MODEL="ARM Cortex A35"
fi

# CPU cores
V_CPU_CORES_ONLINE=$(( $(lscpu -b -e=CPU | wc -l) -1 ))
V_CPU_CORES_OFFLINE=$(( $(lscpu -c -e=CPU | wc -l) -1 ))

V_CPU_CORES="${V_CPU_CORES_ONLINE} online"
if [[ ! ${V_CPU_CORES_OFFLINE} == "-1" ]]; then
  V_CPU_CORES+=" - ${V_CPU_CORES_OFFLINE} offline"
fi

# CPU frequency
if [[ ${V_DEVICE} =~ RG552 ]]; then
  V_CPUFREQL=$(cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq)
  if [ ${V_CPUFREQL} == "<unknown>" ]; then
    V_CPUFREQL="offline"
  else
    V_CPUFREQL=$(( ${V_CPUFREQL} / 1000 ))"MHz"
  fi
  V_CPUMAXFREQL=$(( $(cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq) / 1000 ))
  V_CPUFREQB=$(cat /sys/devices/system/cpu/cpufreq/policy4/cpuinfo_cur_freq)
  if [ ${V_CPUFREQB} == "<unknown>" ]; then
    V_CPUFREQB="offline"
  else
    V_CPUFREQB=$(( ${V_CPUFREQB} / 1000 ))"MHz"
  fi
  V_CPUMAXFREQB=$(( $(cat /sys/devices/system/cpu/cpufreq/policy4/cpuinfo_max_freq) / 1000 ))
else
  V_CPUFREQ=$(( $(cat /sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq) / 1000 ))
  V_CPUMAXFREQ=$(( $(cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq) / 1000 ))
fi

# GPU frequency
if [[ ${V_DEVICE} =~ RG552 ]]; then
  V_GPUFREQ=$(( $(cat /sys/devices/platform/ff9a0000.gpu/devfreq/ff9a0000.gpu/cur_freq) / 1000000 ))
  V_GPUMAXFREQ=$(( $(cat /sys/devices/platform/ff9a0000.gpu/devfreq/ff9a0000.gpu/trans_stat | awk 'NR==2{printf "%s ", $2}') / 1000000 ))
else
  V_GPUFREQ=$(( $(cat /sys/devices/platform/ff400000.gpu/devfreq/ff400000.gpu/cur_freq) / 1000000 ))
  V_GPUMAXFREQ=$(( $(cat /sys/devices/platform/ff400000.gpu/devfreq/ff400000.gpu//trans_stat | awk 'NR==2{printf "%s ", $2}') / 1000000 ))
fi

# RAM frequency
if [[ ${V_DEVICE} =~ RG552 ]]; then
  V_RAMFREQ=$(( $(cat /sys/devices/platform/dmc/devfreq/dmc/target_freq) / 1000000 ))
else
  V_RAMFREQ=$(( $(cat /sys/devices/platform/dmc/devfreq/dmc/cur_freq) / 1000000 ))
fi
V_RAMMAXFREQ=$(( $(cat /sys/devices/platform/dmc/devfreq/dmc/trans_stat | awk 'NR==2{printf "%s ", $(NF-1)}') / 1000000 ))

# battery
BATT=$(cat /sys/class/power_supply/{BAT,bat}*/capacity 2>/dev/null)
BATT_HEALTH=$(cat /sys/class/power_supply/{BAT,bat}*/health 2>/dev/null)
BATT_STATUS=$(cat /sys/class/power_supply/{BAT,bat}*/status 2>/dev/null)

# CPU/GPU temperature
# Unit: millidegree Celsius
if [[ ${V_DEVICE} =~ RG552 ]]; then
  TEMPE=$(cat /sys/class/hwmon/hwmon1/temp1_input 2>/dev/null | sort -rn | head -1 | sed -e s+"[0-9][0-9][0-9]$"++)
  TEMPG=$(cat /sys/class/hwmon/hwmon2/temp1_input 2>/dev/null | sort -rn | head -1 | sed -e s+"[0-9][0-9][0-9]$"++)
else
  TEMPE=$(cat /sys/devices/virtual/thermal/thermal_zone*/temp 2>/dev/null | sort -rn | head -1 | sed -e s+"[0-9][0-9][0-9]$"++)
fi

# RAM usage
RAM=$(echo "$(free --mega | awk 'NR==2{printf "%.0f MB/%.0f MB (%.1f%%)", $3,$2,$3*100/$2}')")

# GAMES partition usage
G_USED=$(df -h /storage/roms | awk 'NR==2 {print $3}')
G_TOTAL=$(df -h /storage/roms | awk 'NR==2 {print $2}')
G_PERCENT="$(echo "$(df /storage/roms | awk 'NR==2{printf "%.1f%%", $3*100/$2}')")"

# SYSTEM partition usage
S_USED=$(df -h /storage | awk 'NR==2 {print $3}')
S_TOTAL=$(df -h /storage | awk 'NR==2 {print $2}')
S_PERCENT="$(echo "$(df /storage | awk 'NR==2{printf "%.1f%%", $3*100/$2}')")"


echo "OPERATING SYSTEM: ${NAME}"
echo "IMAGE: ${COREELEC_DEVICE}"
echo "VERSION: ${VERSION} (${BUILD_ID:0:7})"
echo "BUILD DATE: ${BUILD_DATE}"
if [[ ${R_DEVICE} == "R33S" ]]; then
  echo "DEVICE: Game Console R33S"
elif [[ $R_DEVICE == "R3xS" ]]; then
  echo "DEVICE: Game Console R3xS/PowKiddy RGB20S"
elif [[ $R_DEVICE == "Unknown" ]]; then
  echo "DEVICE: Unknown"
else
  echo "DEVICE: ${V_DEVICE}"
fi
echo "GAMES PARTITION: ${G_USED}B/${G_TOTAL}B (${G_PERCENT})"
echo "SYSTEM PARTITION: ${S_USED}B/${S_TOTAL}B (${S_PERCENT})"
echo "RAM: ${RAM}"
echo "SOC: ${V_SOC}"
echo "CPU MODEL: ${V_CPU_MODEL}"
echo "CPU CORES: ${V_CPU_CORES}"
if test -n "${TEMPE}"
then
  echo "CPU TEMPERATURE: ${TEMPE}°"
fi
if test -n "${V_CPUFREQ}"
then
  echo "CPU CURRENT FREQUENCY: ${V_CPUFREQ}MHz"
  echo "CPU MAXIMUM FREQUENCY: ${V_CPUMAXFREQ}MHz"
else
  echo "CPU CURRENT FREQUENCY (BIG): ${V_CPUFREQB}"
  echo "CPU MAXIMUM FREQUENCY (BIG): ${V_CPUMAXFREQB}MHz"
  echo "CPU CURRENT FREQUENCY (LITTLE): ${V_CPUFREQL}"
  echo "CPU MAXIMUM FREQUENCY (LITTLE): ${V_CPUMAXFREQL}MHz"
fi
if test -n "${TEMPG}"
then
  echo "GPU TEMPERATURE: ${TEMPG}°"
fi
echo "GPU CURRENT FREQUENCY: ${V_GPUFREQ}MHz"
echo "GPU MAXIMUM FREQUENCY: ${V_GPUMAXFREQ}MHz"
echo "RAM CURRENT FREQUENCY: ${V_RAMFREQ}MHz"
echo "RAM MAXIMUM FREQUENCY: ${V_RAMMAXFREQ}MHz"
if test -n "${BATT}"
then
  echo "BATTERY LEVEL: ${BATT}%"
  if test -n "${BATT_HEALTH}"
  then
    echo "BATTERY HEALTH: ${BATT_HEALTH}"
  fi
  if test -n "${BATT_STATUS}"
  then
    echo "BATTERY STATUS: ${BATT_STATUS}"
  fi
fi
echo "KERNEL: ${V_SYSTEM} (${V_ARCH})"
